package com.wsq.nutz.module;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.lang.util.Context;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.wsq.nutz.bean.User;
import com.wsq.nutz.service.UserService;

@IocBean
@At("/user1")//入口函数对应的 URL
@Ok("json")//成功视图
@Fail("http:500")//失败视图
public class UserModule {

	private static Log log = Logs.get();
	
	@Inject 
	private UserService userService;
	
    @At
    @Aop("superAop")
    public Object ping() {
    	log.debug("times At"+Times.sDTms(Times.now()));
    	new Throwable().printStackTrace();
        return Times.sDTms(Times.now());
    }
    
    @GET
    @At("/login")
    @Ok("jsp:jsp.user1.login") // 降内部重定向到登录jsp
    public void loginPage() {}
    
    @At
    // 覆盖UserModule类的@Filter设置,因为登陆可不能要求是个已经登陆的Session
    @POST
    public Object login(@Param("username")String username, 
            @Param("password")String password, 
            @Attr(scope=Scope.SESSION, value="nutz_captcha")String _captcha,
            HttpSession session,ServletContext context) {
		        NutMap re = new NutMap();
		        
		        if (Strings.isBlank(username) || Strings.isBlank(password)) {
		            log.debug("username or password is null");
		            return false;
		        }

		        int userId = userService.fetch(username, password,context);
		        if (userId == 0) {
		            return re.setv("ok", false).setv("msg", "用户名或密码错误");
		        } else {
		            // 完成nutdao_realm后启用.
		            SecurityUtils.getSubject().login(new SimpleShiroToken(userId));
		            session.setAttribute("me", userId);
		            
		            return true;
		        }
    }
    
    @At
    //@RequiresPermissions("user1:add")
    public Object add(@Param("..")User user) { // 两个点号是按对象属性一一设置
        NutMap re = new NutMap();
       // String msg = checkUser(user, true);
/*        if (msg != null){
            return re.setv("ok", false).setv("msg", msg);
        }*/
        user = userService.add(0,user.getName(), user.getPassword());
        return re.setv("ok", true).setv("data", user);
    }
    
    @At
   // @RequiresPermissions("user1:update")
    public Object update(@Param("password")String password, @Attr("me")int me) {
//        if (Strings.isBlank(password) || password.length() < 6)
//            return new NutMap().setv("ok", false).setv("msg", "密码不符合要求");
//      userService.updatePassword(me, password);
    	System.out.println(password+"------------"+me);
        return new NutMap().setv("ok", true);
    }
    
    @At
    @Ok(">>:/")
    public void logout(HttpSession session) {
        session.invalidate();
    }
    
    @At
    @RequiresRoles("admin")  
    public void hello() {  
        //有权限  
    	System.out.println("hasRole++++++++++++++++");
    }  
    
}
